﻿using DemoApplication.Config;
using EntityFrameworkLayer.Entities;
using System;
using System.Windows;

namespace DemoApplication.Dialog
{
    /// <summary>
    /// Interaction logic for MaterialDetailsWindow.xaml
    /// </summary>
    public partial class MaterialDetailsWindow : Window
    {
        private Material m_Material;

        public MaterialDetailsWindow()
        {
            InitializeComponent();
        }

        #region Public functions

        public void SetMaterialId(int id)
        {
            m_Material = DataCommunicator.GetMaterialService().Get(id);
        }

        #endregion

        #region event handling functions

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbbClassify.ItemsSource = Configuration.GetClassifyList();
            cbbCostUnit.ItemsSource = Configuration.GetCostUnitList();
            cbbQuantityUnit.ItemsSource = Configuration.GetQuantityUnitList();

            bool bCreateNew = false;
            if (null == m_Material)
            {
                m_Material = new Material();
                bCreateNew = true;
            }
            spCreate.Visibility = (bCreateNew ? Visibility.Visible : Visibility.Collapsed);
            lblCreate.Visibility = (bCreateNew ? Visibility.Visible : Visibility.Collapsed);
            spOpen.Visibility = (!bCreateNew ? Visibility.Visible : Visibility.Collapsed);
            lblOpen.Visibility = (!bCreateNew ? Visibility.Visible : Visibility.Collapsed);

            this.DataContext = m_Material;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ValidateMaterialData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            DataCommunicator.GetMaterialService().Create(m_Material);
            MessageBox.Show("Add meterial successful");
            this.DialogResult = true;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ValidateMaterialData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            DataCommunicator.GetMaterialService().Update(m_Material);
            MessageBox.Show("Edit meterial successful");
            this.DialogResult = true;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ValidateMaterialData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            if (MessageBoxResult.No == MessageBox.Show("Do you want to delete this material?", "Delete confirmation", MessageBoxButton.YesNo))
            {
                return;
            }

            DataCommunicator.GetMaterialService().Delete(m_Material.Id);
            MessageBox.Show("Delete meterial successful");
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        #endregion

        #region Private functions

        private void ValidateMaterialData()
        {
            if (string.IsNullOrWhiteSpace(m_Material.SerialNumber))
            {
                throw new Exception("Please input Serial number");
            }

            if (string.IsNullOrWhiteSpace(m_Material.Name))
            {
                throw new Exception("Please input Name");
            }

            if (string.IsNullOrWhiteSpace(m_Material.Classify))
            {
                throw new Exception("Please select Classify");
            }

            if (m_Material.Cost <= 0)
            {
                throw new Exception("Cost cannot be smaller than 0");
            }

            if (string.IsNullOrWhiteSpace(m_Material.CostUnit))
            {
                throw new Exception("Please select Cost unit");
            }

            if (m_Material.Quantity <= 0)
            {
                throw new Exception("Quantity cannot be smaller than 0");
            }

            if (string.IsNullOrWhiteSpace(m_Material.QuantityUnit))
            {
                throw new Exception("Please select Quantity unit");
            }
        }

        #endregion
    }
}
